package com.planet.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.planet.goods.dao.BrandMapper;
import com.planet.goods.dao.CategoryMapper;
import com.planet.goods.dao.SkuMapper;
import com.planet.goods.dao.SpuMapper;
import com.planet.goods.pojo.*;
import com.planet.goods.service.SpuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class SpuServicelmpl implements SpuService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private SpuMapper spuMapper;

    @Override
    public void saveGoods(Goods goods) {
        Spu spu = goods.getSpu();
        if(spu.getId()==null){
            //增加
            spu.setId(idWorker.nextId());
            spuMapper.insertSelective(spu);
        }else{
            //修改数据
            spuMapper.updateByPrimaryKeySelective(spu);
            //删除该Spu的Sku
            Sku sku = new Sku();
            sku.setSpuId(spu.getId());
            skuMapper.delete(sku);
        }
        // sku
        Date date = new Date();
        Category category = categoryMapper.selectByPrimaryKey(spu.getCategory3Id());
        Brand brand = brandMapper.selectByPrimaryKey(spu.getBrandId());
        List<Sku> skuList = goods.getSkuList();
        for (Sku sku : skuList){
            if (StringUtil.isEmpty(sku.getSpec())){
                sku.setSpec("{}");
            }
            String name = spu.getName();
            Map<String, String> specMap = JSON.parseObject(sku.getSpec(), Map.class);
            for (Map.Entry<String, String> entity : specMap.entrySet()){
                name += " " + entity.getValue();
            }
            // name
            sku.setName(name);
            sku.setId(idWorker.nextId());
            sku.setBrandName(brand.getName());
            sku.setSpuId(spu.getId());
            sku.setCreateTime(date);
            sku.setUpdateTime(date);
            sku.setCategoryId(category.getId());
            sku.setCategoryName(category.getName());
            skuMapper.insertSelective(sku);
        }

    }

    @Override
    public Goods findGoodsById(Long id) {
        // spu 查询
        Spu spu = spuMapper.selectByPrimaryKey(id);
        // sku 查询
        Sku sku = new Sku();
        sku.setSpuId(spu.getId());
        List<Sku> skuList = skuMapper.select(sku);

        // create goods
        Goods goods = new Goods();
        goods.setSpu(spu);
        goods.setSkuList(skuList);
        return goods;
    }

    /***
     * 商品审核
     * @param spuId
     */
    @Override
    public void audit(Long spuId) {
        //查询商品
        Spu spu = spuMapper.selectByPrimaryKey(spuId);
        //判断商品是否已经删除
        if(spu.getIsDelete().equalsIgnoreCase("1")){
            throw new RuntimeException("该商品已经删除！");
        }
        //实现上架和审核
        spu.setStatus("1"); //审核通过
        spu.setIsMarketable("1"); //上架
        spuMapper.updateByPrimaryKeySelective(spu);
    }

    /**
     * 商品下架
     * @param spuId
     */
    @Override
    public void pull(Long spuId) {
        Spu spu = spuMapper.selectByPrimaryKey(spuId);
        if(spu.getIsDelete().equals("1")){
            throw new RuntimeException("此商品已删除！");
        }
        spu.setIsMarketable("0");//下架状态
        spuMapper.updateByPrimaryKeySelective(spu);
    }

    /***
     * 商品上架
     * @param spuId
     */
    @Override
    public void put(Long spuId) {
        Spu spu = spuMapper.selectByPrimaryKey(spuId);
        //检查是否删除的商品
        if(spu.getIsDelete().equals("1")){
            throw new RuntimeException("此商品已删除！");
        }
        if(!spu.getStatus().equals("1")){
            throw new RuntimeException("未通过审核的商品不能！");
        }
        //上架状态
        spu.setIsMarketable("1");
        spuMapper.updateByPrimaryKeySelective(spu);
    }

    @Override
    @Transactional
    public void logicDelete(Long spuId) {
        Spu spu = spuMapper.selectByPrimaryKey(spuId);
        if(spu.getIsMarketable().equals("1")){
            throw new RuntimeException("上架商品无法删除");
        }
        spu.setIsDelete("1");
        spu.setStatus("0");
        spuMapper.updateByPrimaryKeySelective(spu);
    }

    @Override
    public PageInfo<Spu> findPage(Spu spu, int page, int size) {
        PageHelper.startPage(page, size); // 分页
        Example example = createExample(spu); // 搜索条件
        return new PageInfo<Spu>(spuMapper.selectByExample(example)); // 搜索
    }

    private Example createExample(Spu spu){
        Example example = new Example(Spu.class);
        Example.Criteria criteria = example.createCriteria();
        if(spu!=null){
            if (!StringUtil.isEmpty(spu.getName())){
                criteria.andLike("name", "%"+spu.getName()+"%");
            }
            if (!StringUtil.isEmpty(spu.getSn())){
                criteria.andLike("sn", "%"+spu.getSn()+"%");
            }
            if (spu.getBrandId()!=null){
                criteria.andEqualTo("brand_id", spu.getBrandId());
            }
        }
        return example;
    }

}
